//
//  HELSearchHistoryVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/20.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELSearchHistoryVC.h"
#import "BTSearchBar.h"
#import "NSString+Utilities.h"
#import "SearchTool.h"
#import "HELNormalCell.h"
#import "HELSearchResultVC.h"
#import "JCAlertView.h"
#import "HELFilterheader.h"

@interface HELSearchHistoryVC ()<UISearchBarDelegate>

@property (nonatomic,  strong) BTSearchBar *searchBar;


@end

@implementation HELSearchHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    
}

- (void)configUI{
   
    self.navigationItem.titleView = self.searchBar;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:0 target:self action:@selector(beginSearch)];
    self.navigationItem.rightBarButtonItem = right;
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64);
    [self.tableView registerNib:[UINib nibWithNibName:@"HELNormalCell" bundle:nil] forCellReuseIdentifier:@"HELNormalCell"];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.loading = YES;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.dataArray = [NSMutableArray arrayWithArray: [[[SearchTool getRecentSearchKeyWords] reverseObjectEnumerator] allObjects]];
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.tableView.loading = NO;
            [self.tableView reloadData];
        });
    });
    
}

- (BTSearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar = [BTSearchBar searchBarWithDelegate:self];
      
        
    }
    return _searchBar;
}




- (void)beginSearch {
    
    [self.searchBar resignFirstResponder];
    if (self.searchBar.text && ![self.searchBar.text isEqualToString:@""]) {
        //去掉首位空白字符
       
       
        HELSearchResultVC *resultVC =[[HELSearchResultVC alloc] initWithKeyWord: [self.searchBar.text trim]];
        [self.navigationController pushViewController:resultVC animated:YES];
    }
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELNormalCell"];
    cell.mainTitleL.text = self.dataArray[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  
    UIView *div = [UIView new];
    div.frame = CGRectMake(0, 0, kScreenWidth, 44);
    
    UILabel *label =[UILabel new];
    label.frame = CGRectMake(20, 10, 70, 20 );
    [div addSubview:label];
    label.textColor = kTxtColor;
    label.font = [UIFont systemFontOfSize:16];
    label.text = @"搜索历史";
    
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(kScreenWidth- 64, 10, 44, 20);
    [btn setTitleColor:kTxtColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(removeAll) forControlEvents:UIControlEventTouchUpInside];
    [div addSubview:btn];

    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitle:@"清除" forState:UIControlStateNormal];
    UIView *line =[UIView new];
    line.frame = CGRectMake(0, 43, kScreenWidth, 1);
    line.backgroundColor = kBorderColor;
    [div addSubview:line];
    
    
    return div;
    
  
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    [self beginSearch];
}

- (void)removeAll{
    
    if (!self.dataArray.count) {
        return;
    }
    
 
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否确认清除所有记录" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"不" Click:^{
        
    } ButtonType:JCAlertViewButtonTypeWarn ButtonTitle:@"是" Click:^{
        [self.dataArray removeAllObjects];
        [SearchTool removeAllWord];
        
        
        [self.tableView reloadData];
    }];
    
   
  
    
    
  
    
}



-(NSArray *)tableView:(UITableView* )tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [SearchTool removeAwordForIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
     

    }];//此处是iOS8.0以后苹果最新推出的api，UITableViewRowAction，Style是划出的标签颜色等状态的定义，这里也可自行定义
   
   
    return @[deleteRoWAction];//最后返回这俩个RowAction 的数组
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return YES;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    HELSearchResultVC *resultVC =[[HELSearchResultVC alloc] initWithKeyWord:self.dataArray[indexPath.row]];
    [self.navigationController pushViewController:resultVC animated:YES];
    
    
    
}


@end
